<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>增加</title>
    <link rel="stylesheet" href="${request.contextPath}/statics/layui/css/layui.css" media="all">
</head>
<body>
<form class="layui-form" action="" lay-filter="formTest">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">盐</label>
        <div class="layui-input-block">
            <input type="text" name="salt" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门8ID</label>
        <div class="layui-input-block">
            <input type="text" name="deptId" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <input type="hidden" name="userId" id="userId"  autocomplete="off" placeholder="请输入标题" class="layui-input">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="${request.contextPath}/statics/layui/layui.js"></script>
<script type="text/javascript" src="${request.contextPath}/statics/js/queryUtils.js"></script>
<script>
    var baseURL = "../../";
    layui.use(['form', 'layedit', 'laydate','jquery'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        //监听提交
        form.on('submit(demo1)', function(data){
            /*layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            });*/
            // alert(JSON.stringify(data.field))
            var url = data.field.userId == null ? "DemoController/save" : "DemoController/update";
            console.log(url)
            $.ajax({
                url: baseURL + url,
                dataType: "json",
                type: "post",
                async: false,
                data: data.field,
                success: function(r){
                    if(r.code === 0){
                        /*var i = layer.getFrameIndex(window.name);
                        layer.close(index);  // 关闭页面
                        layer.alert('操作成功', function(index){
                            parent.layui.table.reload("table");
                        });*/
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.layui.table.reload("table");
                    }else{
                        layer.alert(r.msg);
                    }
                }
            });
            return false;
        });

        //操作类型：add新增，edit修改，view查看
        var operateType = QueryUtils.GetQueryString("type");
        var user_id = QueryUtils.GetQueryString("user_id");
        switch (operateType) {
            case "add":
                break;
            case "edit":
                $.get(baseURL + "DemoController/info/"+user_id, function(r){
                    form.val("formTest", {
                        "username": r.data.username// "name": "value"
                        ,"password": r.data.password
                        ,"salt": r.data.salt
                        ,"email": r.data.email
                        ,"mobile": r.data.mobile
                        ,"deptId": r.data.deptId
                        ,"userId": r.data.userId
                    })
                });
                break;
            case "detail":
                $.get(baseURL + "DemoController/info/"+user_id, function(r){
                    console.log(r.data)
                    form.val("formTest", {
                        "username": r.data.username// "name": "value"
                        ,"password": r.data.password
                        ,"salt": r.data.salt
                        ,"email": r.data.email
                        ,"mobile": r.data.mobile
                        ,"deptId": r.data.deptId
                    })
                });
               /* $.ajax({
                    url: baseURL + "demo/demo/info/"+user_id,
                    data: {userId: user_id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (r) {
                        if (r.code === "0") {
                            var list = r.data;
                            console.log(list)
                            //回显
                            form.val("formTest", {
                                "username": "贤心" // "name": "value"
                                ,"password": "女"
                                ,"salt": 3
                                ,"email": true
                                ,"mobile": false
                                ,"deptId": "我爱layui"
                            })
                            form.render();
                        } else if (r.code === "01") {
                            layer.msg(r.msg, function () {
                            });
                        }
                    },
                });*/
                break;
        }
    });
</script>
</body>
</html>